package My.project.Ui.Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import My.project.Entity.Video;
import My.project.Ui.R;
import My.project.Util.Connector;
import android.app.Activity;
import android.app.ListFragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * This fragment is used to display search results of a keyword.
 * @author Bell
 *
 */
public class SearchVideoFragment extends ListFragment {

	int mCurCheckPosition = 0;
    private List<Video> videoList = new ArrayList<Video>();
    private String keyword;
    
   /* @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle bundle = this.getArguments();
        keyword = bundle.getString("keyword");
        
        update(keyword);
        
        if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }
    }*/
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Bundle bundle = this.getArguments();
        keyword = bundle.getString("keyword");
        
        SimpleAdapter adapter = new SimpleAdapter(
        		this.getActivity(), this.getData(), R.layout.vlist, 
        		new String[]{"title","info","img"},
        		new int[]{R.id.title,R.id.info,R.id.img});
        setListAdapter(adapter);        
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v,
    		int position, long id) {
        showVideo(position);
    }

    /**
     * Start a new activity to play the video
     */
    private void showVideo(int index) {
    	Uri uri = Uri.parse(Connector.videoUrl +
    			videoList.get(index).getUrl());
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
    }
    
    private List<Map<String, Object>> getData() {
    	// generate video list
    	Connector con = new Connector();
    	videoList = con.searchVideo(keyword);
    	
    	// generate list to be displayed
        List<Map<String, Object>> list =
        		new ArrayList<Map<String, Object>>();
 
        for (int i = 0; i < videoList.size(); i++) {
        	Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", videoList.get(i).getVideoName());
            map.put("info", videoList.get(i).getDescription());
            map.put("img", R.drawable.content_picture);
            list.add(map);
        }
        return list;
    }
}
